//
//  YBMessageDetailView.m
//  GongXinDangJian
//
//  Created by apple on 17/12/1.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "YBMessageDetailView.h"
#import "YBMessageImageModel.h"
#import "STPhotoBroswer.h"

@interface YBMessageDetailView ()

/**  back  */
@property (strong, nonatomic) UIScrollView  *backScrollView;

@property (nonatomic , strong) UIButton * closeButton;

@property (nonatomic , strong) YBAcceptMessageListModel * model;

@property (nonatomic , strong) NSArray<YBMessageImageModel *> * imageArray;

@property (nonatomic , strong) UILabel * titleLabel;

@property (nonatomic , strong) UILabel * detailLabel;

@property (nonatomic , strong) UIView * imageContentView;

@end

@implementation YBMessageDetailView

- (instancetype)initNormalMessageDetailViewWithMessageModel:(YBAcceptMessageListModel *)messageModel
{
    self = [super initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], [UIScreen zj_screenHeight])];
    if (self) {
        self.model = messageModel;
        self.imageArray = @[];
        [self ybInitMyViewWithIsRequest:YES];
    }
    return self;
}


- (instancetype)initNormalMessageNoRequestDetailViewWithMessageModel:(YBAcceptMessageListModel *)messageModel
{
    self = [super initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], [UIScreen zj_screenHeight])];
    if (self) {
        self.model = messageModel;
        self.imageArray = @[];
        [self ybInitMyViewWithIsRequest:NO];
    }
    return self;
}


-(void)ybInitMyViewWithIsRequest:(BOOL)isRequest{
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    [self addSubview:self.backScrollView];
    
    [self addSubview:self.closeButton];
    self.closeButton.sd_layout.topSpaceToView(self.backScrollView , ItemHeight * 30).centerXEqualToView(self.backScrollView).widthIs(ItemHeight * 88).heightIs(ItemHeight * 88);
    
    [self.backScrollView addSubview:self.titleLabel];
    [self.backScrollView addSubview:self.detailLabel];
    [self.backScrollView addSubview:self.imageContentView];
    
    [self reloadMyView];
    if (isRequest) {
        [self ybInitDataSource];
    }
}


-(void)ybInitDataSource{
    [YBRequestManager request_messageDetailWithMessageID:self.model.ID successBlock:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            self.model = [YBAcceptMessageListModel yy_modelWithJSON:object[@"MSG"]];
            self.imageArray = [NSArray yy_modelArrayWithClass:[YBMessageImageModel class] json:object[@"MSG_RESES"]];
            [self reloadMyView];
        }
    } failBlock:^(id object) {
        
    }];
}



-(void)reloadMyView{
    CGFloat mainWidth = [UIScreen zj_screenWith] - ItemWidth * 100;
    
    CGFloat titleHeight = [self.model.TITLE boundingRectWithSize:CGSizeMake(mainWidth - 1, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : LYPBOLDFONT(22)} context:nil].size.height;
    self.titleLabel.sd_layout.topSpaceToView(self.backScrollView , ItemHeight * 100).leftSpaceToView(self.backScrollView , ItemWidth * 20).widthIs(mainWidth).heightIs(titleHeight + 1);
    [self.titleLabel updateLayout];
    if (titleHeight > LYPBOLDFONT(22).lineHeight) {
        self.titleLabel.textAlignment = NSTextAlignmentLeft;
    }
    else{
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    self.titleLabel.text = [ToolClass returnNoNullStringWithString:self.model.TITLE];
    
    CGFloat detailHeight = [self.model.CONTENT boundingRectWithSize:CGSizeMake(mainWidth - 1, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : LYPFONT(18)} context:nil].size.height;
    self.detailLabel.sd_layout.topSpaceToView(self.titleLabel , ItemHeight * 30).leftSpaceToView(self.backScrollView , ItemWidth * 20).widthIs(mainWidth).heightIs(detailHeight + 1);
    [self.detailLabel updateLayout];
    self.detailLabel.text = [ToolClass returnNoNullStringWithString:self.model.CONTENT];
    
    NSInteger hangCount = (self.imageArray.count % 3 == 0 ? 0 : 1) + self.imageArray.count / 3;
    self.imageContentView.sd_layout.topSpaceToView(self.detailLabel , ItemHeight * 30).leftSpaceToView(self.backScrollView , ItemWidth * 20).widthIs(mainWidth).heightIs(ItemHeight * 170 * hangCount);
    [self.imageContentView updateLayout];
    
    [ToolClass removeAllSubViewFormView:self.imageContentView];
    
    CGFloat imageItemWidth = (mainWidth - ItemWidth * 40) / 3;
    for (int i = 0 ; i < self.imageArray.count ; i ++) {
        YBMessageImageModel * imageModel = self.imageArray[i];
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake((imageItemWidth + ItemWidth * 20) * (i % 3), ItemHeight * 170 * (i / 3), imageItemWidth, ItemHeight * 150)];
        [imageView sd_setImageWithURL:CREATEURL(imageModel.RES_URL) placeholderImage:PlaceholderImage];
        imageView.tag = 500 + i;
        imageView.userInteractionEnabled = YES;
        [self.imageContentView addSubview:imageView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(option_imageView:)];
        [imageView addGestureRecognizer:tap];
    }
    
    [self.backScrollView setupAutoContentSizeWithBottomView:self.imageContentView bottomMargin:ItemHeight * 50];
    
}

-(void)option_imageView:(UITapGestureRecognizer *)gesture{
    UIImageView * imageView = (UIImageView *)gesture.view;
    NSInteger index = imageView.tag - 500;
    NSMutableArray * imageStringArray = [NSMutableArray array];
    for (YBMessageImageModel * imageModel in self.imageArray) {
        [imageStringArray addObject:[NSString stringWithFormat:@"%@/%@" , HTMLURL , imageModel.RES_URL]];
    }
    STPhotoBroswer *brower = [[STPhotoBroswer alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], [UIScreen zj_screenHeight]) ImageArray:[imageStringArray copy] currentIndex:index];
    [brower show];

}


-(void)option_closeButton:(UIButton *)sender{
    [self removeFromSuperview];
}

- (UIScrollView *)backScrollView{
    if (!_backScrollView ) {
        _backScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(ItemWidth * 30 , navHeight + ItemHeight * 10, [UIScreen zj_screenWith] - ItemWidth * 60, [UIScreen zj_screenHeight] - navHeight * 2 - ItemHeight * 20)];
        _backScrollView.showsVerticalScrollIndicator = NO;
        _backScrollView.showsHorizontalScrollIndicator = NO;
        _backScrollView.contentSize = _backScrollView.size;
        _backScrollView.backgroundColor = [UIColor themeGrayColor];
    }
    return _backScrollView;
    
}


-(UIButton *)closeButton
{
    if (!_closeButton) {
        _closeButton = [ToolClass createButtonWithFrame:CGRectMake(0, 0, 1, 1) text:@"" textColor:[UIColor whiteColor] imageName:@"message_btn_close" font:15];
//        _closeButton.clipsToBounds = YES;
//        _closeButton.layer.cornerRadius = ItemHeight * 60;
        [_closeButton addTarget:self action:@selector(option_closeButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeButton;
}

-(UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [ToolClass creatLabelWithFrame:CGRectMake(0, 0, 1, 1) font:22 textColor:[UIColor textBlackColor] text:@""];
        _titleLabel.font = LYPBOLDFONT(22);
    }
    return _titleLabel;
}

-(UILabel *)detailLabel
{
    if (!_detailLabel) {
        _detailLabel = [ToolClass creatLabelWithFrame:CGRectMake(0, 0, 1, 1) font:18 textColor:[UIColor textBlackColor] text:@""];
    }
    return _detailLabel;
}


-(UIView *)imageContentView
{
    if (!_imageContentView) {
        _imageContentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        _imageContentView.backgroundColor = [UIColor themeGrayColor];
    }
    return _imageContentView;
}


@end
